﻿// ***********************************************************************
// Apache 2.0 License
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.Models
// File      : YModuleAttachmentModel.cs
//
// ***********************************************************************
using ContractMeow.Api.Dtos.Attachment;
using ContractMeow.Api.Dtos.Extensions;
using System;

namespace ContractMeow.Models.Attachments
{
	public class YModuleAttachmentModel : AttachmentModel, IYModuleAttachmentModel
	{
		private Uri _inlineUrl;
		private Uri _thumbnailUrl;

		protected YModuleAttachmentModel()
		{

		}

		internal YModuleAttachmentModel(IYModuleAttachment attachment) :
			base(attachment)
		{
			ThumbnailUrl = attachment.ThumbnailUrl.ToUri();
			InlineUrl = attachment.InlineUrl.ToUri();

			InlineHtml = attachment.InlineHtml;
			RecordId = attachment.RecordId;
			RealId = attachment.RealId;
			RealType = attachment.RealType;
			YModule = YModuleModel.FromYModule(attachment.YModule);
		}

		public static IYModuleAttachmentModel FromIYModuleAttachment(IYModuleAttachment attachment)
		{
			return new YModuleAttachmentModel(attachment);
		}

		public Uri InlineUrl
		{
			get { return _inlineUrl; }
			set
			{
				_inlineUrl = value;
			}
		}

		public Uri ThumbnailUrl
		{
			get { return _thumbnailUrl; }
			set
			{
				_thumbnailUrl = value;
			}
		}

		public bool HasThumbnail
		{
			get { return ThumbnailUrl != null; }
		}

		public string InlineHtml { get; set; }
		public long? RecordId { get; set; }
		public long? RealId { get; set; }
		public string RealType { get; set; }
		public YModuleModel YModule { get; set; }

		public static YModuleAttachmentModel FromAttachmentModel(AttachmentModel attachment)
		{
			return attachment == null
					   ? null
					   : new YModuleAttachmentModel()
						   {
							   CreatedAt = attachment.CreatedAt,
							   Description = attachment.Description,
							   Id = attachment.Id,
							   GroupId = attachment.GroupId,
							   Url = attachment.Url,
							   WebUrl = attachment.WebUrl,
							   Name = attachment.Name,
							   Size = attachment.Size,
							   Type = attachment.Type,
						   };
		}
	}
}
